package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.CustomerException;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.mapper.SetmealMapper;
import com.itheima.reggie.pojo.Setmeal;
import com.itheima.reggie.pojo.SetmealDish;
import com.itheima.reggie.service.SetmealDishService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
public class SetmealServiceImpl extends ServiceImpl<SetmealMapper, Setmeal> implements SetmealService {

    @Autowired
    private SetmealDishService setmealDishService;
    @Autowired
    private SetmealMapper setmealMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveWithDish(SetmealDto setmealDto) {
        //1. 先保存套餐信息setmeal
        this.save(setmealDto);
        //2. 再保存关联菜品数据（给每个SetmealDish设置套餐id）
        setmealDto.getSetmealDishes().forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        //批量保存套餐菜品关联数据
        setmealDishService.saveBatch(setmealDto.getSetmealDishes());
    }

    @Override
    public Page<SetmealDto> pageSetmealDto(Integer page, Integer pageSize, String name) {
        Page<SetmealDto> pageInfo = new Page<>(page, pageSize);
        setmealMapper.pageSetmealDto(pageInfo, name);
        return pageInfo;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteByIds(Long[] ids) {
        //1. 判断套餐是否正在售卖 select count(*) from setmeal where id in ( ) and status=1
        Integer count = this.lambdaQuery()
                .in(Setmeal::getId, Arrays.asList(ids))
                .eq(Setmeal::getStatus, 1)
                .count(); //查询条数
        if (count > 0){
            //抛出异常
            throw new CustomerException("有套餐正在售卖，不能删除！");
        }
        //2. 删除套餐，removeByIds根据主键删除
        this.removeByIds(Arrays.asList(ids));

        //3. 删除套餐菜品关联数据 delete from setmeal_dish where setmeal_id in ( )
        setmealDishService.lambdaUpdate()
                .in(SetmealDish::getSetmealId, Arrays.asList(ids))
                .remove();
    }

    @Override
    public SetmealDto getSetmealWithDish(Long id) {
        return setmealMapper.getSetmealWithDish(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWithDish(SetmealDto setmealDto) {
        //1. 更新套餐信息
        this.updateById(setmealDto);
        //2. 更新套餐菜品信息
        List<SetmealDish> setmealDishes = setmealDto.getSetmealDishes();
        //2.1 删除套餐对应的菜品关关联数据 delete from setmeal_dish where setmeal_id=?
        setmealDishService.lambdaUpdate()
                .eq(SetmealDish::getSetmealId, setmealDto.getId())
                .remove();
        //2.2 给每个SetmealDish设置套餐id
        setmealDishes.forEach(setmealDish -> setmealDish.setSetmealId(setmealDto.getId()));
        //2.3 批量保存
        setmealDishService.saveBatch(setmealDishes);
    }
}
